//
//  RestaurantDetailParser.m
//  RegBars
//
//  Created by Huu Nhat on 7/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RestaurantDetailParser.h"
#import "SBJSON.h"

#define BarsNode @"bars"
#define BarNode @"bar"
#define NameNode @"name"
#define AddressNode @"adresse"
#define StreetNode @"strasse"
#define ZipNode @"plz"
#define CityNode @"ort"
#define PhoneNode @"tel"
#define EvenNode @"oeffnungszeiten"
#define TimeNode @"zeit"


@implementation RestaurantDetailParser
@synthesize  delegate;
@synthesize resultData;
@synthesize resDetailDict;

- (void)loadRestaurantList:(NSString *)url withDelegate:(id)aDelegate{
    self.delegate = aDelegate;
    NSString *urlString = [NSString stringWithFormat:@"%@",RestaurantListURL];
	NSURL *baseURL = [NSURL URLWithString:[[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] retain]];	
    NSLog(@"baseURL : %@",baseURL);
	NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
	theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    resultData = [[NSMutableData alloc]init ];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.resultData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    [delegate didEndGetRestaurantsDetail:nil];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    SBJSON *jsonParser = [[[SBJSON alloc] init] autorelease];
    NSString *jsonFileContent = [[[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding] autorelease];
    NSMutableArray *resultArray = (NSMutableArray*)[[jsonParser objectWithString:jsonFileContent error:nil] retain];
    if (resultArray == nil) {
        if ([self.delegate respondsToSelector:@selector(didEndGetRestaurantsDetail:)]) {
            [delegate didEndGetRestaurantsDetail:nil];
        }
    }else {
        self.resDetailDict = [[NSMutableDictionary alloc] init];
        for (NSInteger i = 0; i < [resultArray count]; i++) {
            NSDictionary *dict = [resultArray objectAtIndex:i];
            NSLog(@"RestaurantsDetail :%@", dict);
            NSString *barid = [dict objectForKey:@"barid"];
            RestaurantDetail *restaurantDetail = [[[RestaurantDetail alloc] init] autorelease];
            NSDictionary *addDict = [dict objectForKey:AddressNode];
            [restaurantDetail setCity:[addDict objectForKey:CityNode]];
            [restaurantDetail setZipcode:[addDict objectForKey:ZipNode]];
            [restaurantDetail setPhone:[addDict objectForKey:PhoneNode]];
            [restaurantDetail setAddress:[addDict objectForKey:StreetNode]];
            NSDictionary *timeDict = [dict objectForKey:EvenNode];
            [restaurantDetail setSpecialTime:[NSMutableArray arrayWithObject:[timeDict objectForKey:TimeNode]]];
            [resDetailDict setObject:restaurantDetail forKey:barid];
        }
        if ([self.delegate respondsToSelector:@selector(didEndGetRestaurantsDetail:)]) {
            [self.delegate didEndGetRestaurantsDetail:resDetailDict];
        }
    }
}
/*
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
	NSString *contentString = [NSString stringWithFormat:@"%@",self.currentString];
	[self.currentString setString:@""];
	if ([elementName isEqualToString:BarNode]){
		[self.currentRestaurantName setString:@""];
		[restarantsDict setObject:restaurantDetail forKey:[NSString stringWithFormat:@"%d",currentBarId]];
		[restaurantDetail release];
	}
	if ([elementName isEqualToString:StreetNode]) {
		[restaurantDetail setAddress:contentString];
	}
	if ([elementName isEqualToString:ZipNode]) {
		[restaurantDetail setZipcode:contentString];
	}
	if ([elementName isEqualToString:CityNode]) {
		[restaurantDetail setCity:contentString];
	}
	if ([elementName isEqualToString:PhoneNode]) {
		[restaurantDetail setPhone:contentString];
	}
	if ([elementName isEqualToString:TimeNode]) {
		[restaurantDetail.specialTime addObject:contentString];
	}
	if ([elementName isEqualToString:@"barid"]) {
		currentBarId = [contentString intValue];
	}
}
*/

- (void)dealloc {
    [resultData release];
    [resDetailDict release];
	[super dealloc];
}

@end
